package com.padakeji.android.pworks.ui;

import android.app.DialogFragment;
import android.app.FragmentManager;
import android.os.Bundle;
import com.padakeji.android.pworks.R;
import com.padakeji.android.pworks.app.PadaActivity;

/**
 * Created by AtanL on 2015/5/16.
 */
public class AlertWrapperActivity extends PadaActivity {

    private static ThreadLocal<DialogFragment> localFrag = new ThreadLocal<DialogFragment>();

    /**
     * 设置需要被Alert的DialogFragment。
     * @param frag
     */
    protected static void setShowingFrag(DialogFragment frag) {
        localFrag.set(frag);
    }

    private DialogFragment mFragmentDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        mFragmentDialog = localFrag.get();

        if (mFragmentDialog == null) {
            throw new RuntimeException("AlertWrapperActivity.setShowingFrag(DialogFragment) must be invoked to set DialogFragment to be shown.");
        }

        super.onCreate(savedInstanceState);
        setContentView(R.layout.pada_alert_wrapper);
        alertDialogFragment(mFragmentDialog);
        getFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
            @Override
            public void onBackStackChanged() {
                finish();
            }
        });

    }

    private void alertDialogFragment(DialogFragment frag) {
        frag.show(getFragmentManager(), frag.getTag());
    }

}
